package com.neusoft.elm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.dao.impl.FoodDaoImpl;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.service.FoodService;
import com.neusoft.elm.util.DBUtil;

public class FoodServiceImpl implements FoodService{

	
	@Override
	   public List<Food> listFoodByBusinessId(Integer businessId) {
	       List<Food> list = new ArrayList<>();
	       FoodDao dao = new FoodDaoImpl();
	       try {
	           DBUtil.getConnection();
	           list = dao.listFoodByBusinessId(businessId);
	       }catch (Exception e) {
	           e.printStackTrace();
	       }finally {
	           DBUtil.close();
	       }
	       return list;
	   }
	
	// 商家管理获取食品列表（增强版本）
    @Override
    public Map<String, Object> listFoodForManage(Integer businessId, 
                                               String searchKeyword, 
                                               String sortField, 
                                               int page, 
                                               int pageSize) {
        Map<String, Object> result = new HashMap<>();
        FoodDao dao = new FoodDaoImpl();
        try {
            DBUtil.getConnection();
            
            // 获取食品列表
            List<Food> foods = dao.listFoodForManage(
                businessId, searchKeyword, sortField, page, pageSize);
            
            // 获取总数
            int totalItems = dao.countFoodByBusinessId(businessId, searchKeyword);
            
            result.put("foods", foods);
            result.put("totalItems", totalItems);
            result.put("code", 200);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("message", "加载食品列表失败: " + e.getMessage());
        } finally {
            DBUtil.close();
        }
        return result;
    }
   
   @Override
   public int saveFood(Food food) {
       int result = 0;
       FoodDao dao = new FoodDaoImpl();
       try {
           DBUtil.getConnection();
           result = dao.saveFood(food);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           DBUtil.close();
       }
       return result;
   }
   
   @Override
   public int updateFood(Food food) {
       int result = 0;
       FoodDao dao = new FoodDaoImpl();
       try {
           DBUtil.getConnection();
           result = dao.updateFood(food);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           DBUtil.close();
       }
       return result;
   }
   
   @Override
   public int removeFood(Integer foodId) {
       int result = 0;
       FoodDao dao = new FoodDaoImpl();
       try {
           DBUtil.getConnection();
           result = dao.removeFood(foodId);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           DBUtil.close();
       }
       return result;
   }
   
   @Override
   public Food getFoodById(Integer foodId) {
       Food food = null;
       FoodDao dao = new FoodDaoImpl();
       try {
           DBUtil.getConnection();
           food = dao.getFoodById(foodId);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           DBUtil.close();
       }
       return food;
   }
}

